package cn.xiongxf.order.controller;

import cn.xiongxf.order.domain.OrderDto;
import cn.xiongxf.order.service.IOrderPetAcquisitionService;
import cn.xiongxf.order.domain.OrderPetAcquisition;
import cn.xiongxf.order.query.OrderPetAcquisitionQuery;
import cn.xiongxf.basic.util.AjaxResult;
import cn.xiongxf.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {

    @Autowired
    public IOrderPetAcquisitionService orderPetAcquisitionService;

    /**
     * 接口：添加或修改
     * @param orderPetAcquisition  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderPetAcquisition orderPetAcquisition){
        try {
            if( orderPetAcquisition.getId()!=null)
                orderPetAcquisitionService.update(orderPetAcquisition);
            else
                orderPetAcquisitionService.add(orderPetAcquisition);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            orderPetAcquisitionService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
                orderPetAcquisitionService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public OrderPetAcquisition findById(@PathVariable("id")Long id) {
        return orderPetAcquisitionService.findById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<OrderPetAcquisition> findAll(){
        return orderPetAcquisitionService.findAll();
    }


    /**
     * 接口：分页查询或高级查询
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<OrderPetAcquisition> queryPageList(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request) {
        return orderPetAcquisitionService.queryPageByRole(query,request);
    }

    /**
     * 接口：确认订单  【处理寻主消息 - 发送短信给店铺收购人员 - 店铺收购人员到用户哪里去接宠物 - 订单完成 -确认订单】
     * @param orderDto
     * @return AjaxResult 响应给前端
     */
    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody OrderDto orderDto){
        try {
            orderPetAcquisitionService.confirm(orderDto);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"订单确认失败");
        }
    }
}
